﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;

namespace DemoAdaboost
{
    public partial class CameraCaptureMultiple : Form
    {
        private Capture capture;
        private bool captureInProgress;

        public CameraCaptureMultiple()
        {
            InitializeComponent();

            #region if capture is not created, create it now
            if (capture == null)
            {
                try
                {
                    capture = new Capture();
                    //capture.ImageGrabbed += ProcessFrame;
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
            #endregion
        }

        private void ProcessFrame(object sender, EventArgs arg)
        {
            //Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
            //captureImageBox.Image = ImageFrame;

            Image<Bgr, Byte> frame = capture.QueryFrame();// RetrieveBgrFrame();

            Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>();
            Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
            Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
            Image<Gray, Byte> cannyFrame = smoothedGrayFrame;//.Canny(100, 60);

            captureImageBox.Image = frame;
            grayscaleImageBox.Image = grayFrame;
            smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
            cannyImageBox.Image = cannyFrame;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            

            if (capture != null)
            {
                if (captureInProgress)
                {  //if camera is getting frames then stop the capture and set button Text
                    // "Start" for resuming capture
                    btnStart.Text = "Start!"; //
                    Application.Idle -= ProcessFrame;
                }
                else
                {
                    //if camera is NOT getting frames then start the capture and set button
                    // Text to "Stop" for pausing capture
                    btnStart.Text = "Stop";
                    Application.Idle += ProcessFrame;
                }

                captureInProgress = !captureInProgress;
            }
        }

        private void ReleaseData()
        {
            if (capture != null)
                capture.Dispose();
        }

        private void btnHorizontal_Click(object sender, EventArgs e)
        {
            if (capture != null) 
                capture.FlipHorizontal = !capture.FlipHorizontal;
        }

        private void btnVertical_Click(object sender, EventArgs e)
        {
            if (capture != null) 
                capture.FlipVertical = !capture.FlipVertical;
        }


    }
}
